package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.MyTheadLocal;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@RequestMapping("/addressBook")
@RestController
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    @PostMapping
    public R<AddressBook> address(@RequestBody AddressBook addressBook){

        addressBook.setUserId(MyTheadLocal.get());

        addressBookService.save(addressBook);

        return R.success(addressBook);
    }

    @PutMapping("/default")
    public R isDefault(@RequestBody AddressBook addressBook){

        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();

        qw.eq(AddressBook::getUserId,MyTheadLocal.get());

        qw.set(AddressBook::getIsDefault,0);

        addressBook.setIsDefault(1);

        addressBookService.update(qw);

        //addressBookService.save(addressBook);

        addressBookService.updateById(addressBook);

        return R.success(addressBook);

    }

    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id){

        AddressBook addressBook = addressBookService.getById(id);

        if(addressBook != null){

            return R.success(addressBook);

        }

        return R.error("没有找到该对象");

    }

    @GetMapping("/default")
    public R selectIsdefault(HttpSession session){

        //User user = (User) session.getAttribute("loginPhone");

        Long userId = MyTheadLocal.get();

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();

        qw.eq(AddressBook::getIsDefault,1);

        qw.eq(AddressBook::getUserId,userId);

        AddressBook one = addressBookService.getOne(qw);

        if (one != null){

            return R.success(one);

        }

        return R.error("当前用户没有默认地址");

    }

    @GetMapping("/list")
    public R selectList(HttpSession session){

        Long userId = MyTheadLocal.get();

        System.out.println("======================"+userId);

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();

        qw.eq(AddressBook::getUserId,userId);

        List<AddressBook> list = addressBookService.list(qw);

        if (list != null){

            return R.success(list);

        }

        return R.error("当前用户没有地址");

    }

    @DeleteMapping
    public R deleteAddress(@RequestParam List<String> ids){

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();

        qw.in(AddressBook::getId,ids);

        addressBookService.remove(qw);

        return R.success("删除成功");
    }

    @PutMapping
    public R update(@RequestBody AddressBook addressBook){

        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();

        qw.eq(AddressBook::getUserId,MyTheadLocal.get());

        qw.set(AddressBook::getIsDefault,0);

        addressBookService.update(qw);

        addressBook.setIsDefault(1);

        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }


}
